package com.itheima.web.servlet;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.service.impl.BrandServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet {
    private BrandService brandService = new BrandServiceImpl();

    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Brand> brands = brandService.selectAll();
        String jsonString = JSON.toJSONString(brands);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader br = request.getReader();
        String param = br.readLine();
        Brand brand = JSON.parseObject(param, Brand.class);
        brandService.add(brand);
        response.getWriter().write("success");
    }

    public void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id = request.getParameter("id");
        brandService.deleteById(Integer.parseInt(id));

        response.getWriter().write("success");
    }

    public void selectOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id = request.getParameter("id");

        Brand brand = brandService.selectOne(Integer.parseInt(id));

        String jsonString = JSON.toJSONString(brand);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    public void updateBrandById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        BufferedReader br = request.getReader();
        String param = br.readLine();
        Brand brand = JSON.parseObject(param, Brand.class);
        brandService.updateBrandById(brand);
        response.getWriter().write("success");
    }
    public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        BufferedReader br = request.getReader();
        String param = br.readLine();
//        System.out.println("param->" + param);
        int[] ids = JSON.parseObject(param, int[].class);
        brandService.deleteByIds(ids);
        response.getWriter().write("success");
    }

    public void selectByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");

        int begin = Integer.parseInt(_currentPage);
        int size = Integer.parseInt(_pageSize);

        PageBean<Brand> pageBean = brandService.selectByPage(begin,size);
        String jsonString = JSON.toJSONString(pageBean);

        System.out.println(jsonString);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");

        int begin = Integer.parseInt(_currentPage);
        int size = Integer.parseInt(_pageSize);

        BufferedReader br = request.getReader();
        String param = br.readLine();

        System.out.println("param->" + param);

        Brand brand = JSON.parseObject(param, Brand.class);



        PageBean<Brand> pageBean = brandService.selectByPageAndCondition(begin,size,brand);
        String jsonString = JSON.toJSONString(pageBean);

        System.out.println(jsonString);

        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }


}
